var http = require('http')
var fs = require('fs')

var server = http.createServer((req, res) => {
    console.log(req.url, req.method)
    if (req.url === '/login') {
        switch (req.method) {
            case "GET":
                // fs.createReadStream('login.html').pipe(res);
                fs.readFile(__dirname + '/login.html', (err, data) => {
                    if (err) {
                        // 文件不存在反回异常
                        res.end('Flie is not found!')
                        throw err
                    }
                    res.writeHead(200, {
                        'Content-Type': 'text/html'
                    })
                    res.end(data)
                })
                break;
            case "POST":
                var body = []
                req.on('data', chunk => {
                    body.push(chunk)
                }).on('end', () => {
                    body = Buffer.concat(body).toString()
                    console.log(body)
                    res.end(body)
                });
                break;
            default:
                console.log('other method')
        }
    } else {
        res.writeHead(302, {
            'Location': '/login'
        })
        res.end()
    }
})
server.listen(3000, ()=> {console.log('服务运行在3000端口')})